<?php
@session_start();
$access_token = $_SESSION['access_token'];

$suser = $access_token['user_id'];


?>
<h1>Create a new Form</h1>

<div>Use this page to create a new form. Add new fields via the "Add Field" button.</b></div><br />

<form method="post" action="_createform.php" onsubmit="return doSubmit();">
<table>
<tr><td>Title:</td></tr>
<tr><td><input type = "text" name="title" value="" /></td></tr>
<tr><td>Description:</td></tr>
<tr><td><textarea rows="5" cols="35" name="formdesc" /></textarea></td></tr>
<tr><td>Fields:</td></tr>
<tr><td>Type:</td></tr>
<tr><td><select id="formfield">
	<option value="1">Small Text (10 characters)</option>
	<option value="2">Large Text (25 characters)</option>
	<option value="3">Numeric (4 characters)</option>
	<option value="5">Choice selection (3 characters)</option>
<?php/*	<option value="4">Color Picker (7 characters)</option>
	<option value="1">File Upload (18 characters)</option>
	<option value="1">Music Entry (25 characters)</option>
	<option value="1">Hyperlink (18 characters)</option>
*/
?>
	</select></td></tr>
<tr><td>Label:</td></tr>
<tr><td><input type = "text" id="fieldlabel" value="" /></td></tr>
<tr><td><button type="button" onclick="addField()">Add Field</button></td></tr>
<tr><td><div id="fieldHolder"></div></td></tr>
<tr><td align="right"><input type = "submit" name="submit_invest"  value="Send" /><input type="reset" /></td></tr>
</table>
<input type = "hidden" name="formvalues" id="formvalues" value="" />
<input type = "hidden" name="suser" value="<?php echo $suser; ?>" />
</form>

 <script type="text/javascript">
function addField()
{
	var fieldType = document.getElementById("formfield");
	var fieldLabel = document.getElementById("fieldlabel");
	var fieldHolder = document.getElementById("fieldHolder");

	var controlType = fieldType.options[fieldType.selectedIndex].value;
	var controlName = fieldType.options[fieldType.selectedIndex].text;

	var divContain = document.createElement("div");
	var ancDelete = document.createElement("input");
	var brBreak = document.createElement("br");
	
	ancDelete.type="button";
	ancDelete.onclick = function(){return this.parentNode.parentNode.removeChild(this.parentNode);}
	ancDelete.value = "Delete";

	divContain.innerHTML = "Type: " + controlName + " Label: " + fieldLabel.value + "<br />";
	divContain.title = "Type: " + controlType + " Label: " + fieldLabel.value;
	divContain.appendChild(ancDelete);
	divContain.id="rowOn";

	if (controlType == 5 )
	{
		var divContain2 = document.createElement("div");
		divContain.appendChild(divContain2);
		divContain2.innerHTML = "Choices: <br />";
		var inpChoiceText = document.createElement("input");
		inpChoiceText.type="text";
		var inpChoices = document.createElement("input");
		inpChoices.type="button";
		inpChoices.onclick = function(){return addChoice(this.parentNode);}
		inpChoices.value = "Add Choice";
	

		divContain2.appendChild(inpChoiceText);
		divContain2.appendChild(inpChoices);



	}

	fieldHolder.appendChild(divContain);
	fieldHolder.appendChild(brBreak);

}
function addChoice(obj)
{
	var divContain = document.createElement("div");
	var ancDelete = document.createElement("input");
	
	ancDelete.type="button";
	ancDelete.onclick = function(){return this.parentNode.parentNode.removeChild(this.parentNode);}
	ancDelete.value = "Delete";

	divContain.innerHTML =  obj.getElementsByTagName("input")[0].value;
	divContain.title = obj.getElementsByTagName("input")[0].value;
	divContain.appendChild(ancDelete);
	obj.appendChild(divContain);
}
function doSubmit()
{

	//need to loop thru added fields
	var fieldHolder = document.getElementById("fieldHolder");
	var inpformvalues = document.getElementById("formvalues");

	var sValue = "";

	var thedivs = fieldHolder.getElementsByTagName("div");
	for (var i = 0; i < thedivs.length; i++)
	{ 
    		var rawcommand = thedivs[i].getAttribute("title");
		if (rawcommand==null)
			continue;
		var iType = rawcommand.substr(6, 1);
		if (iType=="")
			continue;
		var sLabel = rawcommand.substr(15);
		if (sLabel=="")
			continue;

		var sChoices = "0";
		if (iType == "5")
		{
			sChoices = "array(";
			//choice selection
			var thechoices = thedivs[i].getElementsByTagName("div");
			for (var y = 0; y < thechoices.length; y++)
			{ 
				var thechoice = thechoices[y].getAttribute("title");
				if (thechoice == null)
					continue;
				sChoices+="\"" + thechoice + "\", ";
			}
			sChoices+=")";
		}
		sValue += "array(" + iType + ", \"" + sLabel + "\", "+sChoices+"),";
	}

	inpformvalues.value = sValue;
	
	return true;

}
</script>
